﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RequestParser;
using System.Text.RegularExpressions;
using System.Globalization;

namespace Parser
{
    public class ParseRequest
    {
        public Request Parse(string strRequest)
        {
            Request ret = new Request();




            //Listing #:      980
            //Description:    Los Pinos, Peak 9, Breckenridge, Northwest, Colorado, USA, Luxury 2 Bedroom Ski-Out Condo - Ideal for Family Ski Vacation
            //Web Address    http://www.vrbo.com/980
            //Arrival Date:   March 3, 2010
            //Departure Date: March 7, 2010
            //Number of Nights:       4
            //# in Party:     6
            //Name:   Angela
            //Email:  afonte2@gmail.com
            //Phone:  9854135877
            //Comments:       wanted to get a price rate from you for these days... thanks!
           var haRegex =
                new Regex(@"Name\t (?<Name>.*)\r(\W*)Requested Dates\t (?<ArrivalDate>.*?) - (?<DepartureDate>.*)\r(\W*)Guests\t (?<NumInParty>.*?)\r(\W*)Phone\t(?<Phone>.*?)\r\W*Email\t(?<Email>.*)");
                Match haMatch = haRegex.Match(strRequest);
                if (haMatch.Length != 0)
                {
                    ret.WebAddress = "http://www.homeaway.com/vacation-rental/p287289";
                    ret.ArrivalDate = DateTime.Parse(haMatch.Groups["ArrivalDate"].Value);
                    ret.DepartureDate = DateTime.Parse(haMatch.Groups["DepartureDate"].Value);
                    ret.NumNights = (ret.DepartureDate - ret.ArrivalDate).Days;
                    ret.NumInParty = haMatch.Groups["NumInParty"].Value.Trim();
                    ret.Name = CultureInfo.CurrentCulture.TextInfo.ToTitleCase(haMatch.Groups["Name"].Value.Trim().Trim());
                    ret.Email = haMatch.Groups["Email"].Value.Trim();
                    ret.Phone = haMatch.Groups["Phone"].Value.Trim();
                    ret.Comments = "";
                }
                else
                {
                    var ohaRegex =
                        new Regex(
                            @"Name\t (?<Name>.*)\r(\W*)Requested Dates\t (?<ArrivalDate>.*?) - (?<DepartureDate>.*)\r(\W*)Guests\t (?<NumInParty>.*?)\r(\W*)Phone\t(?<Phone>.*?)\r\W*Email\t(?<Email>.*)");
                    Match ohaMatch = ohaRegex.Match(strRequest);
                    if (ohaMatch.Length != 0)
                    {
                        ret.WebAddress = "http://www.homeaway.com/vacation-rental/p287289";
                        ret.ArrivalDate = DateTime.Parse(haMatch.Groups["ArrivalDate"].Value);
                        ret.DepartureDate = DateTime.Parse(haMatch.Groups["DepartureDate"].Value);
                        ret.NumNights = (ret.DepartureDate - ret.ArrivalDate).Days;
                        ret.NumInParty = haMatch.Groups["NumInParty"].Value.Trim();
                        ret.Name =
                            CultureInfo.CurrentCulture.TextInfo.ToTitleCase(haMatch.Groups["Name"].Value.Trim().Trim());
                        ret.Email = haMatch.Groups["Email"].Value.Trim();
                        ret.Phone = haMatch.Groups["Phone"].Value.Trim();
                        ret.Comments = "";
                    }
                    else
                    {
                        var nohaRegex =
                            new Regex(
                                @"Name\t (?<Name>.*)\r(\W*)Requested Dates\t (?<ArrivalDate>.*?) - (?<DepartureDate>.*)\r(\W*)Guests\t (?<NumInParty>.*?)\r\W*Email\t(?<Email>.*)");
                        Match nohaMatch = nohaRegex.Match(strRequest);
                        if (nohaMatch.Length != 0)
                        {
                            ret.WebAddress = "http://www.homeaway.com/vacation-rental/p287289";
                            ret.ArrivalDate = DateTime.Parse(nohaMatch.Groups["ArrivalDate"].Value);
                            ret.DepartureDate = DateTime.Parse(nohaMatch.Groups["DepartureDate"].Value);
                            ret.NumNights = (ret.DepartureDate - ret.ArrivalDate).Days;
                            ret.NumInParty = nohaMatch.Groups["NumInParty"].Value.Trim();
                            ret.Name =
                                CultureInfo.CurrentCulture.TextInfo.ToTitleCase(nohaMatch.Groups["Name"].Value.Trim().Trim());
                            ret.Email = nohaMatch.Groups["Email"].Value.Trim();
                            ret.Phone = nohaMatch.Groups["Phone"].Value.Trim();
                            ret.Comments = "";
                        }
                        
                    }

                }
            return ret;
        }
    }
}
